<template>
  <div>
  <el-dialog
    :title="isUpdate?'退货单代填 - 修改订单':'退货单代填 - 新增订单 '"
    :visible="visible"
    width="80%"
    height="600px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
      <!-- 搜索表单 -->
      <el-form
        :model="form"
        label-width="90px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :md="5" :sm="5">
            <el-form-item label="退货日期:">
              <el-date-picker v-model="form.sjthrq" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="4">
            <el-form-item label="带量采购标志:" label-width="100px">
              <el-select
                clearable
                v-model="form.dlcgbz"
                placeholder="请选择订单类型"
                class="ele-fluid">
                <el-option v-for="item in ddlxList" :label="item.label" :value="item.value" :key="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="10" :md="10">
            <el-form-item label="客户名称:">
              <el-input style="margin-top: 5px" disabled
                v-model="form.customername"
                placeholder="">
                <template slot="append">
                  <el-button
                    type="primary"
                    icon="el-icon-search"
                    class="ele-btn-icon"
                    size="small"
                    @click="openSelectCustomer">选择
                  </el-button>
                </template>

              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="5" :md="5">
            <el-form-item label="医院编码:" >
              <el-input
                disabled
                v-model="form.yybm"
                class="ele-fluid">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :lg="4" :md="4">
            <el-form-item label="配送点编码:" label-width="90px">
              <el-input
                disabled
                v-model="form.psdbm"
                class="ele-fluid">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="4">
            <el-form-item label="商品类型:">
              <el-select
                clearable
                v-model="form.splx"
                placeholder="请选择是否补填订单"
                class="ele-fluid">
                <el-option label="药品类" value="1"></el-option>
                <el-option label="医用耗材器械类" value="2"></el-option>
                <el-option label="中药饮片类" value="4"></el-option>
                <el-option label="其他" value="9"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :md="6">
            <el-form-item label="统编代码:" label-width="90px">
              <el-input style="margin-top: 5px"
                v-model="form.zxspbm" disabled
                class="ele-fluid">
                <template slot="append">
                  <el-button
                    type="primary"
                    icon="el-icon-search"
                    class="ele-btn-icon"
                    size="small"
                    @click="openSelectInv">选择
                  </el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :md="6">
            <el-form-item label="规格包装:" label-width="90px">
              <el-input disabled
                v-model="form.ggbz"
                class="ele-fluid">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :lg="4" :md="4">
            <el-form-item label="生产批号:" label-width="90px">
              <el-input
                disabled
                v-model="form.scph"
                class="ele-fluid">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="5" :md="5">
            <el-form-item label="采购计量单位:" label-width="100px">
              <el-select
                clearable
                v-model="form.cgjldw"
                placeholder=""
                class="ele-fluid">
                <el-option label="计价单位" value="1"></el-option>
                <el-option label="最小使用单位" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="5" :md="5">
            <el-form-item label="退货数量:" label-width="90px">
              <el-input @keyup="calcThzj"
                v-model="form.thsl"
                class="ele-fluid">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="5" :md="5">
            <el-form-item label="退货单价:" label-width="90px">
              <el-input @keyup="calcThzj"
                v-model="form.thdj"
                class="ele-fluid">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="5" :md="5">
            <el-form-item label="退货总价:" label-width="90px">
              <el-input
                disabled
                v-model="form.thzj"
                class="ele-fluid">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :lg="24" :md="24">
            <el-form-item label="退货原因:">
              <el-input
                clearable
                v-model="form.thyy"
                placeholder=""/>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
    <ele-pro-table
      ref="tableLog"
      :where="whereLog"
      :datasource="logList"
      :columns="columnsLog"
      :need-page="false"
      height="calc(100vh - 460px)">
      <!-- 表头工具栏 -->
      <template slot="toolbar">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-save"
          class="ele-btn-icon"
          @click="saveOrder()"
          v-if="permission.includes('sys:yq007:add')">保存订单
        </el-button>
        <el-button
          size="small"
          type="warning"
          icon="el-icon-save"
          class="ele-btn-icon"
          @click="submitOrder()"
          v-if="permission.includes('sys:yq007:add')">提交订单
        </el-button>
        <el-button
          size="small"
          type="warning"
          icon="el-icon-save"
          class="ele-btn-icon"
          @click="confirmOrder()"
          v-if="permission.includes('sys:yq007:add')">确认订单
        </el-button>
      </template>
      <template slot="submitNo" slot-scope="{row}">
        <div style="white-space:pre-wrap">{{ row.yqid }}</div>
      </template>
      <template slot="submitTime" slot-scope="{row}">
        <div style="white-space:pre-wrap">{{ row.ts }}</div>
      </template>
      <template slot="reqBody" slot-scope="{row}">
        <el-input type="textarea" :rows="4" :value="row.postxml"></el-input>
      </template>
      <template slot="resBody" slot-scope="{row}">
        <el-input type="textarea" :rows="4" :value="row.retpostxml"></el-input>
      </template>
      <template slot="resResult" slot-scope="{row}">
        <el-input type="textarea" :rows="4" :value="row.retmemo"></el-input>
      </template>
    </ele-pro-table>

    <!-- 编辑弹窗 -->
  </el-dialog>
    <SelectCustomer
      :data="current"
      :visible.sync="showSelectCustomer"
      @done="setCustcode"/>
    <SelectInv
      :yybm="this.yybm"
      :visible.sync="showSelectInv"
      @done="loadInv"/>
  </div>
    <!-- <LogList
    :dataItem="current"
    :visible.sync="showLog"
    /> -->
    <!-- <BillDetail
    :dataItem="current"
    :visible.sync="showDetail"
    /> -->

</template>

<script>
import { mapGetters } from "vuex";
// import LogList from '../../purchase/inbound/loglist.vue';
//import BillDetail from './billdetail.vue';
import SelectCustomer from '../yq005/select-customer';
import SelectInv from './select-inv'

export default {
  name: 'ygorder_orderedit',
  // components: {C37InfCkScBillNewEdit},
  components: {SelectCustomer,SelectInv},
  computed: {
    ...mapGetters(["permission"]),
  },
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表格数据接口
      url: '/yyq005/index',
      isUpdate:false,
      where: {},
      invList:[],
      whereLog: {},
      logList:[],
      xylxName:'',
      addressList:[
      ],
      xylxList:[
        {
          label:'全量响应',
          value:'00'
        },
        {
          label:'部分响应',
          value:'01'
        },
        {
          label:'不予响应',
          value:'02'
        },
      ],
      ddlxList:[
        {
          label:'否',
          value:'0'
        },
        {
          label:'是',
          value:'1'
        },
      ],

      // 表格列配置
      columns: [
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center'
        },
        {
          prop: 'invcode',
          label: '药品代码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'left'
        },
        {
          prop: 'ypname',
          label: '药品名称',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'zxspbm',
          label: '统编代码',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'ggbz',
          label: '规格',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'measname',
          label: '单位',
          showOverflowTooltip: true,
          minWidth: 60,
          align: 'center'
        },
        {
          prop: 'medicinalprocuder',
          label: '生产商',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'cgsl',
          label: '数量',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center',
          slot:'cgsl'
        },
        {
          prop: 'cgdj',
          label: '单价',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center',
          slot:'cgdj'
        },
        {
          prop: 'sfjj',
          label: '是否加急',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center',
          slot:'sfjj'
        },
        {
          prop: 'cwyq',
          label: '仓位要求',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center',
          slot:'cwyq'
        },
        {
          prop: 'psyq',
          label: '配送要求',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center',
          slot:'psyq'
        },

        //   {
        //   columnKey: 'action',
        //   label: '操作',
        //   width: 160,
        //   align: 'center',
        //   resizable: false,
        //   slot: 'action',
        //   fixed: "right"
        // }
      ],
      columnsLog: [
        // {
        //   columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',
        //   fixed: "left"
        // },
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center'
        },
        {
          prop: 'yqid',
          label: '提交编号',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
          slot:'submitNo'
        },
        {
          prop: 'postxml',
          label: '提交Xml',
          showOverflowTooltip: true,
          minWidth: 400,
          align: 'center',
          slot:'reqBody'
        },
        {
          prop: 'retpostxml',
          label: '返回Xml',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          slot:'resBody'
        },
        {
          prop: 'ztcljg',
          label: '处理结果',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'retmemo',
          label: '提示内容',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'ts',
          label: '提交时间',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'createUsraccount',
          label: '提交人',
          showOverflowTooltip: true,
          minWidth: 60,
          align: 'center'
        },

      ],
      // 表格搜索条件
      form: {
        orderdate:new Date(),
        status:'0',
        dlcgbz:'0',
        salesman: this.$store.state.user.user.realname,
        statusdesc:'新建',
        customername:'',
        shaddress:'',
        bzsm:'代填订单',
        zxspbm:'',
        yybm:'',
        psdbm:'',
        cgjldw:'1',
        realorderdate:'',
        splx:'1',
        replaceno:''

      },
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      yybm: '',
      replaceno:'',
      // 是否显示编辑弹窗
      showEdit: false,
      showLog: false,
      showDetail:false,
      showSelectCustomer:false,
      showSelectInv:false,
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.loadlog();
        this.loaddetail();
        this.isUpdate = true;
      } else {
        this.form.replaceno="";
        this.form.customername = "";
        this.form.shaddress= "";


        this.isUpdate = false;
        // 清空省市区控件
        this.invList = [];
        this.logList = [];
      }
    }
  },
  methods: {
    statusFormat(row, column,cellValue) {
      const daterc = row[column.property]
      if(daterc !=null){
        return cellValue=="N"?"已同步":"未同步";
      }
    },
    getFromDate() {
      const timeOne = new Date()
      const year = timeOne.getFullYear()
      let month = timeOne.getMonth()
      let day = 1
      //month = month < 10 ? '0' + month : month
      //day = day < 10 ? '0' + day : day
      ///const NOW_MONTHS_AGO = `${year}-${month}-${day}`
      return new Date(year,month,day);
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {riqiBegin:this.getFromDate(),riqiEnd:new Date()};
      this.reload();
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    openSelectCustomer(){
      this.addressList = [];
      this.form.shaddress = "";
      this.showSelectCustomer = true;
    },
    openSelectInv(){
      this.showSelectInv = true;
    },
    loadInv(data)
    {
      //this.invList = data;
      //console.log(data);
      if(data){
        this.form.zxspbm = data.zxspbm;
        this.form.scph = data.vlotno;
        this.form.ggbz = '';
      }
      //console.log(data);
    },
    submitOrder(){
      if(this.replaceno=='')
      {
        this.$message.info('请先保存订单后再提交。');
        return;
      }
      let param = {
        yq005:this.form,
        yq005Details:this.invList
      };

      this.$http.post('/yyq007/dddtupload', param).then(res => {
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          //this.replaceno = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
        //加载日志列表
        this.loadlog();
      }).catch(e => {
        this.$message.error(e.message);
      });

    },
    confirmOrder(){
      let param = {
        yq005:this.form,
        yq005Details:this.invList
      };

      this.$http.post('/yyq007/dddtconfirm', param).then(res => {
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          //this.replaceno = res.data.data;
          //加载日志列表
          this.loadlog();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });

    },
    loaddetail(){
      this.$http.get('/yyq005/detail?replaceno='+this.form.replaceno).then(res => {
        if (res.data.code === 0) {
          //加载明细列表
          this.invList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });

    },
    async loadlog(){
      var res = await this.$http.get('/yyq005/postlog?postid='+this.form.replaceno);
      if(res.data.code==0)
      {
        this.logList = res.data.data;
      }
    },
    saveOrder(){
      if(!this.form.sjthrq)
      {
        this.$message.info('补填订单必须选择实际采购日期。');
        return;
      }
      if(!this.form.customername)
      {
        this.$message.info('请先选择客户。');
        return;
      }
      if(!this.form.splx)
      {
        this.$message.info('商品类型不能为空，请选择。');
        return;
      }
      if(!this.form.thsl)
      {
        this.$message.info('退货数量不能为空，请选择。');
        return;
      }else{
        if(isNaN(this.form.thsl))
        {
          this.$message.info('请输入有效的退货数量');
          return;
        }
      }
      if(!this.form.thdj)
      {
        this.$message.info('退货单价不能为空，请选择。');
        return;
      }else{
        if(isNaN(this.form.thdj))
        {
          this.$message.info('请输入有效的退货单价');
          return;
        }
      }
      this.$http.post('/yyq007/add', this.form).then(res => {
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.updateVisible(true);
          //this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    setCustcode(custcode,psdbm,custname,addrname)
    {
      this.form.customername = custname;

      this.form.shaddress = addrname;
      this.yybm = custcode;
      this.form.yybm = custcode;
      this.form.psdbm = psdbm;
      //var url = '/yyq005/custaddrlist?ts=1';
      // url+="&custcode="+custcode;
      // this.$http.get(url).then(res=>{
      //   if (res.data.code === 0) {
      //     this.addressList = res.data.data;
      //     if(this.addressList.length>0)
      //     {
      //       this.form.shaddress = this.addressList[0].addrname;
      //     }
      //   }
      //   else
      //   {
      //     this.$message.error(res.data.msg);
      //   }
      //
      // });


    },
    calcThzj()
    {
      if(this.form.thsl && this.form.thdj)
      {
        this.form.thzj = Number(this.form.thsl)*Number(this.form.thdj);
      }
    },
    openLog(row){
      console.log(row);
      this.current = row;
      this.showLog = true;
    },
    openDetail(row){
      this.current = row;
      this.showDetail = true;
    },
    optionClick(data){
      var item = this.xylxList.find((item)=>item.value===data)
      this.xylxName = item.label;
    },
    /* 订单响应 */
    responseOrder(){
      if(!this.where.xylx)
      {
        this.$message.info('请选择需要提交的的订单响应类型。');
        return;
      }
      if(this.selection.length==0)
      {
        this.$message.info('请选择需要响应的阳光平台订单。');
        return;
      }
      this.$confirm('确定要将选中的订单进行【'+this.xylxName+'】吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/ygorder/orderresponse', {ddmxbhs:this.selection.map(d => d.ddmxbh),czlx:this.where.xylx}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 显示编辑 */
    exportToNC() {
      if(this.selection.length==0)
      {
        this.$message.info('请选择需要提交至NC的单据。');
        return;
      }
      this.$confirm('确定要将选中的单据提交至NC吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/ygorder/saleordernc63', {ddmxbhs:this.selection.map(d => d.ddmxbh),startDate:this.where.riqiBegin}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/c37infckscbillnew/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的销售出库/购进退出上传单表吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/c37infckscbillnew/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped>
.el-row
{
 margin-bottom: -10px;
}
::v-deep .el-table--mini td {
  padding: 2px 0;
}
</style>
